<template>
  <header class="smart-banner">
    <div class="banner-content">
      <h1>智能做题助手</h1>
    </div>
  </header>
  <div class="grid-container">
    <!-- 题目展示区 -->
    <div class="card question-display">
      <div class="card-header">
        <i class="fas fa-book-open"></i>
        <h2>题目展示</h2>
      </div>
      <div class="question-content">
        <div v-if="questionLoading" id="loading-overlay">
          <div class="loading-spinner"></div>
          <div class="loading-text">题目生成中...</div>
        </div>
        <div v-if="!question" class="empty-state">
          <i class="fas fa-file-alt"></i>
          <p>生成一个题目吧</p>
        </div>
        <div v-else >
          <div class="question-info">
            <div class="question-tag type-tag">
              <i class="fas fa-tag"></i>
              <span id="question-type-tag">{{question.questionType}}</span>
            </div>
            <div class="question-tag type-tag">
              <i class="fas fa-tag"></i>
              <span id="question-type-tag">{{question.subject }}</span>
            </div>
            <div v-if="question.difficultyLevel < 3"  class="question-tag difficulty-tag difficulty-easy">
              <i class="fas fa-chart-line"></i>
              <span id="question-difficulty-tag">简单</span>
            </div>
            <div v-else-if="question.difficultyLevel === 3"  class="question-tag difficulty-tag difficulty-medium">
              <i class="fas fa-chart-line"></i>
              <span id="question-difficulty-tag">中等</span>
            </div>
            <div  v-else class="question-tag difficulty-tag difficulty-hard">
              <i class="fas fa-chart-line"></i>
              <span id="question-difficulty-hard">困难</span>
            </div>
          </div>
          <VMarkdownView
              mode="light"
              :content="question.questionContent"
          ></VMarkdownView>
        </div>

      </div>
    </div>

    <!-- 批改结果区 -->
    <div class="card grading-result">
      <div class="card-header">
        <i class="fas fa-check-circle"></i>
        <h2>批改反馈</h2>
      </div>
      <div class="result-content">
        <button class="btn grade-btn" id="grade-btn" @click="generateCor">
          <i class="fas fa-robot"></i> AI批改
        </button>

        <div id="result-container">
          <div v-if="correctionLoading" id="loading-overlay">
            <div class="loading-spinner"></div>
            <div class="loading-text">AI批改中...</div>
          </div>
          <div v-if="correction">
            <div class="result-alert success">
              得分：{{ correction.score }}
            </div>

            <VMarkdownView
                mode="light"
                :content="correction.feedback"
            ></VMarkdownView>
          </div>
          <div v-else class="empty-state">
            <i class="fas fa-file-alt"></i>
            <p>提交答案后点击批改按钮</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 答题区 -->
    <div class="card answer-input">
      <div class="card-header">
        <i class="fas fa-pencil-alt"></i>
        <h2>答题区</h2>
      </div>
      <div v-if="submitLoading" id="loading-overlay">
        <div class="loading-spinner"></div>
        <div class="loading-text">答案提交中...</div>
      </div>
      <VMarkdownEditor
          v-model="answer.answerContent"
          locale="zh"
          style="height: 300px"
      />
<!--      <textarea id="user-answer" placeholder="在此输入您的答案..."></textarea>-->
      <button class="btn" id="submit-btn" @click="submitAns">
        <i class="fas fa-paper-plane"></i> 提交答案
      </button>
    </div>

    <!-- 题目生成区 -->
    <div class="card question-generator">
      <div class="card-header">
        <i class="fas fa-robot"></i>
        <h2>AI题目生成</h2>
      </div>
      <div class="generator-input">
        <textarea  placeholder="请输入生成题目要求" v-model="message"></textarea>
      </div>
      <button class="btn btn-accent" @click="generateQues">
        <i class="fas fa-magic"></i> 生成题目
      </button>

      <div class="ai-tips">
        <i class="fas fa-lightbulb"></i>
        <span>提示：可指定知识点+难度（如"三角函数+高难度"）</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { VMarkdownView, VMarkdownEditor } from 'vue3-markdown'
import 'vue3-markdown/dist/style.css'
import { generateQuestion } from '@/api/question'
import { submitAnswer } from '@/api/answer'
import { generateCorrection } from '@/api/correction'

const message = ref('')

const questionLoading = ref(false)
const submitLoading = ref(false)
const correctionLoading = ref(false)

const question = ref(null)

const answer = ref({
  answerId: 0,
  questionId: 0,
  studentId: 1,
  answerContent: ''
})

const correction = ref(null)

function submitAns() {
  answer.value.questionId = question.value.questionId
  submitLoading.value = true
  submitAnswer(answer.value).then(res => {
    answer.value.answerId = res.data
    submitLoading.value = false
  })
}

const output = ref('')

// const sse = inject('$sse');
// const sseClient = sse.create({
//   url: '/api/sse-events',
//   format: 'json' // 自动解析JSON
// });
//
// sseClient.on('message', (data) => {
//   messages.value.push(data);
// });

function generateQues() {
  console.log(message.value)
  questionLoading.value = true
  generateQuestion(message.value).then(res => {
    console.log(res.data)
    questionLoading.value = false
    question.value = res.data
  })
}

function generateCor() {
  correctionLoading.value = true
  generateCorrection(answer.value.answerId).then(res => {
    console.log(res.data)
    correctionLoading.value = false
    correction.value = res.data
  })
}

</script>

<style scoped>
:root {
  --primary: #2c3e50;
  --secondary: #3498db;
  --accent: #f1c40f;
  --success: #27ae60;
  --danger: #e74c3c;
  --light: #ecf0f1;
  --dark: #2c3e50;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

body {
  background: linear-gradient(135deg, #1a2a6c, #2c3e50, #4a569d);
  min-height: 100vh;
  padding: 20px;
}
.smart-banner {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 60px;
  background: linear-gradient(135deg, #1a2a6c, #2c3e50);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  box-shadow: 0 2px 10px rgba(0,0,0,0.3);
}

.banner-content h1 {
  font-size: 1.8rem;
  font-weight: 600;
  letter-spacing: 1px;
}

.grid-container {
  display: grid;
  margin: 80px 20px 20px;
  grid-template-columns: 1fr 0.8fr;
  grid-template-rows: 1fr 1fr;
  gap: 20px;
  height: 95vh;
}

.card {
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(241, 196, 15, 0.15);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  padding: 25px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 20px 30px rgba(0, 0, 0, 0.15);
}

.card-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 2px solid rgba(212, 175, 55, 0.5);
}

.card-header h2 {
  color: var(--primary);
  font-size: 1.8rem;
  margin-left: 15px;
}

.card-header i {
  font-size: 2rem;
  color: var(--primary);
}

/* 题目展示区 */
.question-display {
  grid-area: 1 / 1 / 2 / 2;
}
/* 加载动画覆盖层 */
#loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

/* 旋转动画 */
.loading-spinner {
  width: 50px;
  height: 50px;
  border: 4px solid rgba(241, 196, 15, 0.3); /* 主色调为金色，与主题一致 */
  border-top: 4px solid #f1c40f; /* 顶部边框为金色，形成旋转效果 */
  border-radius: 50%;
  animation: spin 1.5s linear infinite;
}

/* 加载文字 */
.loading-text {
  margin-top: 15px;
  font-size: 1.1rem;
  color: #2c3e50;
  font-weight: 500;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.question-content {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
}

/* 新增：题目信息标签 */
.question-info {
  display: flex;
  gap: 15px;
  margin-bottom: 15px;
  flex-wrap: wrap;
}

.question-tag {
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.9rem;
  font-weight: 600;
  display: flex;
  align-items: center;
}

.question-tag i {
  margin-right: 5px;
}

.type-tag {
  background: rgba(52, 152, 219, 0.15);
  color: #2980b9;
  border: 1px solid rgba(52, 152, 219, 0.3);
}

.difficulty-tag {
  background: rgba(231, 76, 60, 0.15);
  color: #c0392b;
  border: 1px solid rgba(231, 76, 60, 0.3);
}

.difficulty-easy {
  background: rgba(39, 174, 96, 0.15);
  color: #27ae60;
  border: 1px solid rgba(39, 174, 96, 0.3);
}

.difficulty-medium {
  background: rgba(241, 196, 15, 0.15);
  color: #f39c12;
  border: 1px solid rgba(241, 196, 15, 0.3);
}

.difficulty-hard {
  background: rgba(231, 76, 60, 0.15);
  color: #e74c3c;
  border: 1px solid rgba(231, 76, 60, 0.3);
}

.question-content h3 {
  font-size: 1.5rem;
  margin-bottom: 20px;
  color: var(--dark);
}


/* 答题区 */
.answer-input {
  grid-area: 2 / 1 / 3 / 2;
}

textarea {
  width: 100%;
  height: 150px;
  padding: 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 1.1rem;
  resize: none;
  margin-bottom: 20px;
  transition: border-color 0.3s;
}

textarea:focus {
  outline: none;
  border-color: var(--secondary);
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}

.btn {
  padding: 14px 25px;
  background: var(--secondary);
  color: blue;
  border: none;
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

.btn i {
  margin-right: 10px;
}

.btn-accent {
  background: var(--accent);
  color: var(--dark);
}

.btn-accent:hover {
  background: #f39c12;
}

/* 批改结果区 */
.grading-result {
  grid-area: 1 / 2 / 2 / 3;
}

.result-content {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
}

.grade-btn {
  background: var(--success);
  margin-bottom: 20px;
}

.grade-btn:hover {
  background: #219653;
}

.result-alert {
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.success {
  background: rgba(39, 174, 96, 0.15);
  border-left: 5px solid var(--success);
}

.error {
  background: rgba(231, 76, 60, 0.15);
  border-left: 5px solid var(--danger);
}

.result-alert i {
  font-size: 2.5rem;
  margin-right: 15px;
}

.result-alert-content h3 {
  font-size: 1.4rem;
  margin-bottom: 10px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #7f8c8d;
}

.empty-state i {
  font-size: 3rem;
  margin-bottom: 15px;
}

/* 题目生成区 */
.question-generator {
  grid-area: 2 / 2 / 3 / 3;
}

.generator-input {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-bottom: 20px;
}

input, select {
  padding: 14px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 1rem;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--secondary);
}

.ai-tips {
  background: rgba(212, 175, 55, 0.1);
  border-radius: 8px;
  padding: 12px;
  margin-top: 15px;
  font-size: 0.9rem;
  color: var(--dark);
  display: flex;
  align-items: center;
}

.ai-tips i {
  margin-right: 10px;
  color: var(--primary);
}

.loading-indicator {
  text-align: center;
  padding: 20px;
  color: var(--secondary);
  font-size: 1.1rem;
}

.loading-indicator i {
  animation: spin 1s linear infinite;
  margin-right: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .grid-container {
    grid-template-columns: 1fr;
    grid-template-rows: repeat(4, auto);
    height: auto;
  }

  .question-display { grid-area: 1 / 1 / 2 / 2; }
  .grading-result { grid-area: 2 / 1 / 3 / 2; }
  .answer-input { grid-area: 3 / 1 / 4 / 2; }
  .question-generator { grid-area: 4 / 1 / 5 / 2; }

  .card {
    min-height: 400px;
  }
}
</style>
